rm(list = ls())
#working_folder<- "/Users/bgpopescu/Library/CloudStorage/Dropbox/covid_institutions/"
working_folder<-"//Mac/Dropbox-1/covid_paper_replication/"
setwd(working_folder)

#This is to obtain:
#-table_A1

library("readxl")
library("reshape")
library("stringi")
library("stringr")
library("zoo")
library("dplyr")
library("tidyr")
library("stargazer")
library("RColorBrewer")
library("lemon")
library("ggplot2")
library("lfe")
library("readr")
library("geojsonsf")
library(xtable)

data_cov_mob = geojson_sf("./data/data_cov_mob.geojson")
data_cov_mob$pct_manufacturing<-as.numeric(data_cov_mob$pct_manufacturing)
data_nocovid_nomob<-subset(data_cov_mob, week=="00")
data_nocovid_nomob$kul<-as.numeric(data_nocovid_nomob$kul)



###############
#LITS 2016 GIS#
###############

lits_shape_2016 <- st_read(dsn="./data/shape_files.gdb",
                           layer="lits_shape_2016")

data_2016<-lits_shape_2016
data_2016$treat <- as.numeric( with (data_2016,ifelse((data_2016$treat==1), 1, 0)))
data_2016$dist_east_west_brd<-data_2016$east_west_distance/1000
data_2016$dist1 <- as.numeric( with (data_2016,ifelse(data_2016$treat==1, 1, -1)))
data_2016$dist2 <-data_2016$dist1*data_2016$dist_east_west_brd

data_2016$bfe1<-0
data_2016$bfe1[data_2016$east_west_NEAR_FID==4]<-1
data_2016$bfe2<-0
data_2016$bfe2[data_2016$east_west_NEAR_FID==5]<-1
data_2016$bfe3<-0
data_2016$bfe3[data_2016$east_west_NEAR_FID==6]<-1

###############
#LITS 2010 GIS#
###############

lits_shape_2010 <- st_read(dsn="./data/shape_files.gdb",
                           layer="lits_shape_2010")

data_2010<-lits_shape_2010
data_2010$treat <- as.numeric( with (data_2010,ifelse((data_2010$treat==1), 1, 0)))
data_2010$dist_east_west_brd<-data_2010$east_west_distance/1000
data_2010$dist1 <- as.numeric( with (data_2010,ifelse(data_2010$treat==1, 1, -1)))
data_2010$dist2 <-data_2010$dist1*data_2010$dist_east_west_brd

data_2010$bfe1<-0
data_2010$bfe1[data_2010$east_west_NEAR_FID==4]<-1
data_2010$bfe2<-0
data_2010$bfe2[data_2010$east_west_NEAR_FID==5]<-1
data_2010$bfe3<-0
data_2010$bfe3[data_2010$east_west_NEAR_FID==6]<-1



###########
#LITS 2016#
###########

list2016<-read_dta("./data/2016-LiTS-III.dta", encoding = 'latin1')
Ger_2016<- list2016[ which(list2016$country=="Germany"), ]
rm(list2016)


###########
#LITS 2010#
###########

list2010<-read_dta("./data/2010 - lits2.dta", encoding = 'latin1')
Ger_2010<- list2010[ which(list2010$country_=="Germany"), ]
rm(list2010)

############################################################
#SPORT AND RECREATIONAL ORGANIZATIONS AND ASSOCIATIONS 2016#
############################################################

#GR1
Ger_2016$member_sport<-Ger_2016$q919b
Ger_2016$member_sport[Ger_2016$q919b==1] <- 1
Ger_2016$member_sport[Ger_2016$q919b==2] <- 1
Ger_2016$member_sport[Ger_2016$q919b==3] <- 0

#GR2
Ger_2016$member_sport_active_passive<-NA
Ger_2016$member_sport_active_passive[Ger_2016$q919b==1] <- 1
Ger_2016$member_sport_active_passive[Ger_2016$q919b==2] <- 0

#GR3
Ger_2016$member_sport_active_nomb<-NA
Ger_2016$member_sport_active_nomb[Ger_2016$q919b==1] <- 1
Ger_2016$member_sport_active_nomb[Ger_2016$q919b==3] <- 0

#GR4
Ger_2016$member_sport_active_all<-NA
Ger_2016$member_sport_active_all[Ger_2016$q919b==1] <- 1
Ger_2016$member_sport_active_all[Ger_2016$q919b==2] <- 0
Ger_2016$member_sport_active_all[Ger_2016$q919b==3] <- 0

#GR5
Ger_2016$member_sport_passive_active<-NA
Ger_2016$member_sport_passive_active[Ger_2016$q919b==2] <- 1
Ger_2016$member_sport_passive_active[Ger_2016$q919b==1] <- 0

#GR6
Ger_2016$member_sport_passive_nonmb<-NA
Ger_2016$member_sport_passive_nonmb[Ger_2016$q919b==2] <- 1
Ger_2016$member_sport_passive_nonmb[Ger_2016$q919b==3] <- 0

#GR7
Ger_2016$member_sport_passive_all<-NA
Ger_2016$member_sport_passive_all[Ger_2016$q919b==2] <- 1
Ger_2016$member_sport_passive_all[Ger_2016$q919b==1] <- 0
Ger_2016$member_sport_passive_all[Ger_2016$q919b==3] <- 0


############################################################
#SPORT AND RECREATIONAL ORGANIZATIONS AND ASSOCIATIONS 2010#
############################################################

#GR1
Ger_2010$member_sport<-Ger_2010$q713b
Ger_2010$member_sport[Ger_2010$q713b==1] <- 1
Ger_2010$member_sport[Ger_2010$q713b==2] <- 1
Ger_2010$member_sport[Ger_2010$q713b==3] <- 0

#GR2
Ger_2010$member_sport_active_passive<-NA
Ger_2010$member_sport_active_passive[Ger_2010$q713b==1] <- 1
Ger_2010$member_sport_active_passive[Ger_2010$q713b==2] <- 0

#GR3
Ger_2010$member_sport_active_nomb<-NA
Ger_2010$member_sport_active_nomb[Ger_2010$q713b==1] <- 1
Ger_2010$member_sport_active_nomb[Ger_2010$q713b==3] <- 0

#GR4
Ger_2010$member_sport_active_all<-NA
Ger_2010$member_sport_active_all[Ger_2010$q713b==1] <- 1
Ger_2010$member_sport_active_all[Ger_2010$q713b==2] <- 0
Ger_2010$member_sport_active_all[Ger_2010$q713b==3] <- 0

#GR5
Ger_2010$member_sport_passive_active<-NA
Ger_2010$member_sport_passive_active[Ger_2010$q713b==2] <- 1
Ger_2010$member_sport_passive_active[Ger_2010$q713b==1] <- 0

#GR6
Ger_2010$member_sport_passive_nonmb<-NA
Ger_2010$member_sport_passive_nonmb[Ger_2010$q713b==2] <- 1
Ger_2010$member_sport_passive_nonmb[Ger_2010$q713b==3] <- 0

#GR7
Ger_2010$member_sport_passive_all<-NA
Ger_2010$member_sport_passive_all[Ger_2010$q713b==2] <- 1
Ger_2010$member_sport_passive_all[Ger_2010$q713b==1] <- 0
Ger_2010$member_sport_passive_all[Ger_2010$q713b==3] <- 0


####################
#Law obeyance 2016#
####################
Ger_2016$law_obeyance<-Ger_2016$q417d
Ger_2016$law_obeyance[Ger_2016$law_obeyance==-97] <- NA

####################
#Law obeyance 2010#
####################
Ger_2010$law_obeyance<-Ger_2010$q316d
Ger_2010$law_obeyance[Ger_2010$law_obeyance==-97] <- NA


#####
#Age#
#####


#Age 2016
Ger_2016$age_group<-ifelse(Ger_2016$age_pr>=18 & Ger_2016$age_pr<=24,  1,
                           ifelse(Ger_2016$age_pr>=25 & Ger_2016$age_pr<=34, 2,
                                  ifelse(Ger_2016$age_pr>=35 & Ger_2016$age_pr<=44, 3,
                                         ifelse(Ger_2016$age_pr>=45 & Ger_2016$age_pr<=54, 4,
                                                ifelse(Ger_2016$age_pr>=65, 5, NA)))))
#Age 2010
Ger_2010$age_group<-ifelse(Ger_2010$q104a_1>=18 & Ger_2010$q104a_1<=24,  1,
                           ifelse(Ger_2010$q104a_1>=25 & Ger_2010$q104a_1<=34, 2,
                                  ifelse(Ger_2010$q104a_1>=35 & Ger_2010$q104a_1<=44, 3,
                                         ifelse(Ger_2010$q104a_1>=45 & Ger_2010$q104a_1<=54, 4,
                                                ifelse(Ger_2010$q104a_1>=65, 5, NA)))))

#Education 2016
Ger_2016$edu<-ifelse(Ger_2016$q109_1==1, 1,
                     ifelse(Ger_2016$q109_1==2, 2,
                            ifelse(Ger_2016$q109_1==3 | Ger_2016$q109_1==4, 3,
                                   ifelse(Ger_2016$q109_1==5, 4,
                                          ifelse(Ger_2016$q109_1==6, 5,
                                                 ifelse(Ger_2016$q109_1==7, 6, NA))))))
#Education 2010
Ger_2010$q515
Ger_2010$edu<-ifelse(Ger_2010$q515==1, 1,
                     ifelse(Ger_2010$q515==2, 2,
                            ifelse(Ger_2010$q515==3 | Ger_2010$q515==4, 3,
                                   ifelse(Ger_2010$q515==5, 4,
                                          ifelse(Ger_2010$q515==6, 5,
                                                 ifelse(Ger_2010$q515==7, 6, NA))))))


#Income
Ger_2016$income<-as.character(as.factor(Ger_2016$PRq315))
Ger_2016$income<-ifelse(Ger_2016$income=="-97" | Ger_2016$income=="-99", NA, Ger_2016$income)
Ger_2016$income<-as.numeric(Ger_2016$income)
Ger_2010$income<-Ger_2010$q227
Ger_2010$income<-ifelse(Ger_2010$income=="-97" | Ger_2010$income=="-99", NA, Ger_2010$income)

Ger_2010_b<-subset(Ger_2010, select = c(SerialID,
                                        Region1,
                                        member_sport,
                                        member_sport_active_passive,
                                        member_sport_active_nomb,
                                        member_sport_active_all,
                                        member_sport_passive_active,
                                        member_sport_passive_nonmb,
                                        member_sport_passive_all,
                                        law_obeyance,
                                        age_group,
                                        edu,
                                        income,
                                        psu))


Ger_2016_b<-subset(Ger_2016, select = c(ID,
                                        region_name,
                                        member_sport,
                                        member_sport_active_passive,
                                        member_sport_active_nomb,
                                        member_sport_active_all,
                                        member_sport_passive_active,
                                        member_sport_passive_nonmb,
                                        member_sport_passive_all,
                                        law_obeyance,
                                        age_group,
                                        edu,
                                        income,
                                        PSU_name))


Ger_2016_b$ID<-as.character(as.factor(Ger_2016_b$ID))
data_2016$ID<-as.character(data_2016$ID)

data_2016<-left_join(data_2016, Ger_2016_b, by = c("ID"="ID"))
data_2016$POINT_Y<-data_2016$latitude
data_2016$POINT_X<-data_2016$longitude

data_2010<-left_join(data_2010, Ger_2010_b, by = c("SerialID"="SerialID"))
data_2010$POINT_Y<-data_2010$latitude
data_2010$POINT_X<-data_2010$longitude
data_2010$dist_east_west_brd
data_2010$PSU_name<-as.character(data_2010$psu)
data_2010$region_name<-data_2010$Region1



data_2016$wave<-2016
data_2010$wave<-2010
combined<-dplyr::bind_rows(data_2010, data_2016)



mean_mobil <-list("Mobility Changes", length(data_cov_mob$mean_mobil[!is.na(data_cov_mob$mean_mobil)]),
                    round(mean(data_cov_mob$mean_mobil, na.rm=T), digits=3),
                    round(min(data_cov_mob$mean_mobil, na.rm=T), digits=3),
                    round(max(data_cov_mob$mean_mobil, na.rm=T), digits=3),
                    round(sd(data_cov_mob$mean_mobil, na.rm=T), digits=3))

lag_covid_pc <-list("Lag Covid Cases", length(data_cov_mob$lag_covid_pc[!is.na(data_cov_mob$lag_covid_pc)]),
                    round(mean(data_cov_mob$lag_covid_pc, na.rm=T), digits=3),
                    round(min(data_cov_mob$lag_covid_pc, na.rm=T), digits=3),
                    round(max(data_cov_mob$lag_covid_pc, na.rm=T), digits=3),
                    round(sd(data_cov_mob$lag_covid_pc, na.rm=T), digits=3))



kul <-list("Culure Clubs", length(data_nocovid_nomob$kul[!is.na(data_nocovid_nomob$kul)]),
           round(mean(data_nocovid_nomob$kul, na.rm=T), digits=3),
           round(min(data_nocovid_nomob$kul, na.rm=T), digits=3),
           round(max(data_nocovid_nomob$kul, na.rm=T), digits=3),
           round(sd(data_nocovid_nomob$kul, na.rm=T), digits=3))

data_nocovid_nomob$nat<-as.numeric(data_nocovid_nomob$nat)

nat <-list("Nature Clubs", length(data_nocovid_nomob$nat[!is.na(data_nocovid_nomob$nat)]),
           round(mean(data_nocovid_nomob$nat, na.rm=T), digits=3),
           round(min(data_nocovid_nomob$nat, na.rm=T), digits=3),
           round(max(data_nocovid_nomob$nat, na.rm=T), digits=3),
           round(sd(data_nocovid_nomob$nat, na.rm=T), digits=3))

data_nocovid_nomob$spo<-as.numeric(data_nocovid_nomob$spo)

spo <-list("Sport Clubs", length(data_nocovid_nomob$spo[!is.na(data_nocovid_nomob$spo)]),
           round(mean(data_nocovid_nomob$spo, na.rm=T), digits=3),
           round(min(data_nocovid_nomob$spo, na.rm=T), digits=3),
           round(max(data_nocovid_nomob$spo, na.rm=T), digits=3),
           round(sd(data_nocovid_nomob$spo, na.rm=T), digits=3))

data_nocovid_nomob$frei<-as.numeric(data_nocovid_nomob$frei)


frei <-list("Freetime Clubs", length(data_nocovid_nomob$frei[!is.na(data_nocovid_nomob$frei)]),
            round(mean(data_nocovid_nomob$frei, na.rm=T), digits=3),
            round(min(data_nocovid_nomob$frei, na.rm=T), digits=3),
            round(max(data_nocovid_nomob$frei, na.rm=T), digits=3), 
            round(sd(data_nocovid_nomob$frei, na.rm=T), digits=3))


data_nocovid_nomob$soz<-as.numeric(data_nocovid_nomob$soz)

soz <-list("Social Clubs", length(data_nocovid_nomob$soz[!is.na(data_nocovid_nomob$soz)]),
           round(mean(data_nocovid_nomob$soz, na.rm=T), digits=3),
           round(min(data_nocovid_nomob$soz, na.rm=T), digits=3),
           round(max(data_nocovid_nomob$soz, na.rm=T), digits=3),
           round(sd(data_nocovid_nomob$soz, na.rm=T), digits=3))


data_nocovid_nomob$pol<-as.numeric(data_nocovid_nomob$pol)

pol <-list("Political Clubs", length(data_nocovid_nomob$pol[!is.na(data_nocovid_nomob$pol)]),
           round(mean(data_nocovid_nomob$pol, na.rm=T), digits=3),
           round(min(data_nocovid_nomob$pol, na.rm=T), digits=3),
           round(max(data_nocovid_nomob$pol, na.rm=T), digits=3), 
           round(sd(data_nocovid_nomob$pol, na.rm=T), digits=3))

data_nocovid_nomob$inter<-as.numeric(data_nocovid_nomob$inter)

inter <-list("Interest Clubs", length(data_nocovid_nomob$inter[!is.na(data_nocovid_nomob$inter)]),
             round(mean(data_nocovid_nomob$inter, na.rm=T), digits=3),
             round(min(data_nocovid_nomob$inter, na.rm=T), digits=3),
             round(max(data_nocovid_nomob$inter, na.rm=T), digits=3),
             round(sd(data_nocovid_nomob$inter, na.rm=T), digits=3))

data_nocovid_nomob$bridging_tot_pop<-as.numeric(data_nocovid_nomob$bridging_tot_pop)


bridging <-list("Bridging Clubs", length(data_nocovid_nomob$bridging_tot_pop[!is.na(data_nocovid_nomob$bridging_tot_pop)]),
              round(mean(data_nocovid_nomob$bridging_tot_pop, na.rm=T), digits=3),
              round(min(data_nocovid_nomob$bridging_tot_pop, na.rm=T), digits=3),
              round(max(data_nocovid_nomob$bridging_tot_pop, na.rm=T), digits=3),
              round(sd(data_nocovid_nomob$bridging_tot_pop, na.rm=T), digits=3))


data_nocovid_nomob$bonding_tot_pop<-as.numeric(data_nocovid_nomob$bonding_tot_pop)

bonding <-list("Bonding Clubs", length(data_nocovid_nomob$bonding_tot_pop[!is.na(data_nocovid_nomob$bonding_tot_pop)]),
               round(mean(data_nocovid_nomob$bonding_tot_pop, na.rm=T), digits=3),
               round(min(data_nocovid_nomob$bonding_tot_pop, na.rm=T), digits=3),
               round(max(data_nocovid_nomob$bonding_tot_pop, na.rm=T), digits=3),
               round(sd(data_nocovid_nomob$bonding_tot_pop, na.rm=T), digits=3))


data_nocovid_nomob$vereine_tot_pop<-as.numeric(data_nocovid_nomob$vereine_tot_pop)


vereine <-list("All Clubs", length(data_nocovid_nomob$vereine_tot_pop[!is.na(data_nocovid_nomob$vereine_tot_pop)]),
               round(mean(data_nocovid_nomob$vereine_tot_pop, na.rm=T), digits=3),
               round(min(data_nocovid_nomob$vereine_tot_pop, na.rm=T), digits=3),
               round(max(data_nocovid_nomob$vereine_tot_pop, na.rm=T), digits=3),
               round(sd(data_nocovid_nomob$vereine_tot_pop, na.rm=T), digits=3))



pct_afd <-list("Pct AfD, 2017", length(data_nocovid_nomob$pct_afd[!is.na(data_nocovid_nomob$pct_afd)]),
                  round(mean(data_nocovid_nomob$pct_afd, na.rm=T), digits=3),
                  round(min(data_nocovid_nomob$pct_afd, na.rm=T), digits=3),
                  round(max(data_nocovid_nomob$pct_afd, na.rm=T), digits=3),
                  round(sd(data_nocovid_nomob$pct_afd, na.rm=T), digits=3))


pct_afd_2013 <-list("Pct AfD, 2013", length(data_nocovid_nomob$pct_afd_2013[!is.na(data_nocovid_nomob$pct_afd_2013)]),
               round(mean(data_nocovid_nomob$pct_afd_2013, na.rm=T), digits=3),
               round(min(data_nocovid_nomob$pct_afd_2013, na.rm=T), digits=3),
               round(max(data_nocovid_nomob$pct_afd_2013, na.rm=T), digits=3),
               round(sd(data_nocovid_nomob$pct_afd_2013, na.rm=T), digits=3))


change_afd <-list("Change AfD", length(data_nocovid_nomob$pct_change_afd[!is.na(data_nocovid_nomob$pct_change_afd)]),
                  round(mean(data_nocovid_nomob$pct_change_afd, na.rm=T), digits=3),
                  round(min(data_nocovid_nomob$pct_change_afd, na.rm=T), digits=3),
                  round(max(data_nocovid_nomob$pct_change_afd, na.rm=T), digits=3),
                  round(sd(data_nocovid_nomob$pct_change_afd, na.rm=T), digits=3))

lag_covid_pc <-list("Lag Covid Cases", length(data_cov_mob$lag_covid_pc[!is.na(data_cov_mob$lag_covid_pc)]),
                    round(mean(data_cov_mob$lag_covid_pc, na.rm=T), digits=3),
                    round(min(data_cov_mob$lag_covid_pc, na.rm=T), digits=3),
                    round(max(data_cov_mob$lag_covid_pc, na.rm=T), digits=3),
                    round(sd(data_cov_mob$lag_covid_pc, na.rm=T), digits=3))


pct_turnout <-list("Pct. Turnout", length(data_nocovid_nomob$pct_turnout[!is.na(data_nocovid_nomob$pct_turnout)]),
                   round(mean(data_nocovid_nomob$pct_turnout, na.rm=T), digits=3),
                   round(min(data_nocovid_nomob$pct_turnout, na.rm=T), digits=3),
                   round(max(data_nocovid_nomob$pct_turnout, na.rm=T), digits=3),
                   round(sd(data_nocovid_nomob$pct_turnout, na.rm=T), digits=3))


log_pop_total <-list("Log Population Total", length(data_nocovid_nomob$log_pop_total[!is.na(data_nocovid_nomob$log_pop_total)]),
                     round(mean(data_nocovid_nomob$log_pop_total, na.rm=T), digits=3),
                     round(min(data_nocovid_nomob$log_pop_total, na.rm=T), digits=3),
                     round(max(data_nocovid_nomob$log_pop_total, na.rm=T), digits=3),
                     round(sd(data_nocovid_nomob$log_pop_total, na.rm=T), digits=3))

log_gdp_per_cap <-list("Log GDP per cap.", length(data_nocovid_nomob$log_gdp_per_cap[!is.na(data_nocovid_nomob$log_gdp_per_cap)]),
                     round(mean(data_nocovid_nomob$log_gdp_per_cap, na.rm=T), digits=3),
                     round(min(data_nocovid_nomob$log_gdp_per_cap, na.rm=T), digits=3),
                     round(max(data_nocovid_nomob$log_gdp_per_cap, na.rm=T), digits=3),
                     round(sd(data_nocovid_nomob$log_gdp_per_cap, na.rm=T), digits=3))

pop_den <-list("Population Density", length(data_nocovid_nomob$pop_den[!is.na(data_nocovid_nomob$pop_den)]),
               round(mean(data_nocovid_nomob$pop_den, na.rm=T), digits=3),
               round(min(data_nocovid_nomob$pop_den, na.rm=T), digits=3),
               round(max(data_nocovid_nomob$pop_den, na.rm=T), digits=3),
               round(sd(data_nocovid_nomob$pop_den, na.rm=T), digits=3))

pct_college <-list("Pct. College", length(data_nocovid_nomob$pct_college[!is.na(data_nocovid_nomob$pct_college)]),
                   round(mean(data_nocovid_nomob$pct_college, na.rm=T), digits=3),
                   round(min(data_nocovid_nomob$pct_college, na.rm=T), digits=3),
                   round(max(data_nocovid_nomob$pct_college, na.rm=T), digits=3),
                   round(sd(data_nocovid_nomob$pct_college, na.rm=T), digits=3))



pct_pop_above_60 <-list("Pct. Above 60", length(data_nocovid_nomob$pct_pop_above_60[!is.na(data_nocovid_nomob$pct_pop_above_60)]),
                        round(mean(data_nocovid_nomob$pct_pop_above_60, na.rm=T), digits=3),
                        round(min(data_nocovid_nomob$pct_pop_above_60, na.rm=T), digits=3),
                        round(max(data_nocovid_nomob$pct_pop_above_60, na.rm=T), digits=3),
                        round(sd(data_nocovid_nomob$pct_pop_above_60, na.rm=T), digits=3))


pct_pop_under_35 <-list("Pct. Below 35", length(data_nocovid_nomob$pct_pop_under_35[!is.na(data_nocovid_nomob$pct_pop_under_35)]),
                        round(mean(data_nocovid_nomob$pct_pop_under_35, na.rm=T), digits=3),
                        round(min(data_nocovid_nomob$pct_pop_under_35, na.rm=T), digits=3),
                        round(max(data_nocovid_nomob$pct_pop_under_35, na.rm=T), digits=3),
                        round(sd(data_nocovid_nomob$pct_pop_under_35, na.rm=T), digits=3))



pct_service_sec_narrow <-list("Pct. working in Services", length(data_nocovid_nomob$pct_service_sec_narrow[!is.na(data_nocovid_nomob$pct_service_sec_narrow)]),
                              round(mean(data_nocovid_nomob$pct_service_sec_narrow, na.rm=T), digits=3),
                              round(min(data_nocovid_nomob$pct_service_sec_narrow, na.rm=T), digits=3),
                              round(max(data_nocovid_nomob$pct_service_sec_narrow, na.rm=T), digits=3),
                              round(sd(data_nocovid_nomob$pct_service_sec_narrow, na.rm=T), digits=3))


pct_manufacturing <-list("Pct. Manufacturing", length(data_nocovid_nomob$pct_manufacturing[!is.na(data_nocovid_nomob$pct_manufacturing)]),
                         round(mean(data_nocovid_nomob$pct_manufacturing, na.rm=T), digits=3),
                         round(min(data_nocovid_nomob$pct_manufacturing, na.rm=T), digits=3),
                         round(max(data_nocovid_nomob$pct_manufacturing, na.rm=T), digits=3),
                         round(sd(data_nocovid_nomob$pct_manufacturing, na.rm=T), digits=3))

east_ger <-list("East Germany", length(data_nocovid_nomob$east_ger[!is.na(data_nocovid_nomob$east_ger)]),
                round(mean(data_nocovid_nomob$east_ger, na.rm=T), digits=3),
                round(min(data_nocovid_nomob$east_ger, na.rm=T), digits=3),
                round(max(data_nocovid_nomob$east_ger, na.rm=T), digits=3),
                round(sd(data_nocovid_nomob$east_ger, na.rm=T), digits=3))


gender_ratio <-list("Gender Ratio", length(data_nocovid_nomob$gender_ratio[!is.na(data_nocovid_nomob$gender_ratio)]),
                    round(mean(data_nocovid_nomob$gender_ratio, na.rm=T), digits=3),
                    round(min(data_nocovid_nomob$gender_ratio, na.rm=T), digits=3),
                    round(max(data_nocovid_nomob$gender_ratio, na.rm=T), digits=3),
                    round(sd(data_nocovid_nomob$gender_ratio, na.rm=T), digits=3))

pct_students <-list("Pct. Students", length(data_nocovid_nomob$pct_students[!is.na(data_nocovid_nomob$pct_students)]),
                    round(mean(data_nocovid_nomob$pct_students, na.rm=T), digits=3),
                    round(min(data_nocovid_nomob$pct_students, na.rm=T), digits=3),
                    round(max(data_nocovid_nomob$pct_students, na.rm=T), digits=3),
                    round(sd(data_nocovid_nomob$pct_students, na.rm=T), digits=3))


uni_pop <-list("University to Pop. Ratio", length(data_nocovid_nomob$uni_pop[!is.na(data_nocovid_nomob$uni_pop)]),
                    round(mean(data_nocovid_nomob$uni_pop, na.rm=T), digits=3),
                    round(min(data_nocovid_nomob$uni_pop, na.rm=T), digits=3),
                    round(max(data_nocovid_nomob$uni_pop, na.rm=T), digits=3),
                    round(sd(data_nocovid_nomob$uni_pop, na.rm=T), digits=3))


parks_pop <-list("Parks to Pop. Ratio", length(data_nocovid_nomob$parks_pop[!is.na(data_nocovid_nomob$parks_pop)]),
               round(mean(data_nocovid_nomob$parks_pop, na.rm=T), digits=3),
               round(min(data_nocovid_nomob$parks_pop, na.rm=T), digits=3),
               round(max(data_nocovid_nomob$parks_pop, na.rm=T), digits=3),
               round(sd(data_nocovid_nomob$parks_pop, na.rm=T), digits=3))


law_obeyance <-list("Agreemeent Breaking Law", length(data_2016$law_obeyance[!is.na(data_2016$law_obeyance)]),
                    round(mean(data_2016$law_obeyance, na.rm=T), digits=3),
                    round(min(data_2016$law_obeyance, na.rm=T), digits=3),
                    round(max(data_2016$law_obeyance, na.rm=T), digits=3),
                    round(sd(data_2016$law_obeyance, na.rm=T), digits=3))



member_sport <-list("Sport Club Member", length(data_2016$member_sport[!is.na(data_2016$member_sport)]),
                    round(mean(data_2016$member_sport, na.rm=T), digits=3),
                    round(min(data_2016$member_sport, na.rm=T), digits=3),
                    round(max(data_2016$member_sport, na.rm=T), digits=3),
                    round(sd(data_2016$member_sport, na.rm=T), digits=3))


age <-list("Age", length(data_2016$age_group[!is.na(data_2016$age_group)]),
           round(mean(data_2016$age_group, na.rm=T), digits=3),
           round(min(data_2016$age_group, na.rm=T), digits=3),
           round(max(data_2016$age_group, na.rm=T), digits=3),
           round(sd(data_2016$age_group, na.rm=T), digits=3))


income <-list("Income", length(data_2016$income[!is.na(data_2016$income)]),
              round(mean(data_2016$income, na.rm=T), digits=3),
              round(min(data_2016$income, na.rm=T), digits=3),
              round(max(data_2016$income, na.rm=T), digits=3),
              round(sd(data_2016$income, na.rm=T), digits=3))


edu <-list("Education", length(data_2016$income[!is.na(data_2016$edu)]),
              round(mean(data_2016$edu, na.rm=T), digits=3),
              round(min(data_2016$edu, na.rm=T), digits=3),
              round(max(data_2016$edu, na.rm=T), digits=3),
              round(sd(data_2016$edu, na.rm=T), digits=3))



sd<-as.data.frame(rbind(mean_mobil,
                        vereine,
                        bridging,
                        bonding,
                        kul,
                        nat,
                        spo,
                        frei,
                        soz,
                        pol,
                        inter,
                        pct_afd,
                        pct_afd_2013,
                        change_afd,
                        lag_covid_pc,
                        pct_turnout,
                        log_pop_total,
                        log_gdp_per_cap,
                        pop_den,
                        pct_college,
                        pct_pop_above_60,
                        pct_pop_under_35,
                        pct_service_sec_narrow,
                        pct_manufacturing,
                        east_ger,
                        gender_ratio,
                        pct_students,
                        uni_pop,
                        parks_pop,
                        law_obeyance,
                        member_sport,
                        age,
                        income,
                        edu))
colnames(sd)<-c("Variable", "N", "Mean", "Min", "Max", "SD")
rownames(sd) <- NULL

ncol(sd)
object_latex<-xtable(sd, type = "latex", caption = "Summary Statistics", digits=c(0,0,0,3,3,3,3))
align(object_latex) <- xalign(object_latex)

large <- function(x){
  paste0('{\\bfseries ', x, '}') }
italic <- function(x){ paste0('{\\emph{ ', x, '}}')
}


print(object_latex, file = "./paper/tables/table_A1.tex", 
      caption.placement = "top",
      sanitize.colnames.function = large,
      booktabs = TRUE,
      include.rownames=FALSE)

